var gridStore;
 var ids="";
Ext.define('app.controller.AccountController', {
			extend : 'Ext.app.Controller',
			refs : [{
						ref : 'ViewportAccount',
						selector : 'ViewportAccount' 
					}],
			stores : ["Accounts"],
			init : function() {
				this.control({
							'button[name=add]' : {
								click : function(o) {
									var openWindow = Ext.widget('addAccountView');
									openWindow.animateTarget = document.body;
									openWindow.title = "添加客户";
									openWindow.show();
								}
							}, 
							'button[name=edit]' : {
								click : function(o) {
									var rows = o.ownerCt.ownerCt.getSelectionModel().getSelection();// 父组件
									if (rows.length != 1) {
										Ext.MessageBox.alert("提示", "请选择一个分销商");
										return;
									}else{
										var openWindow = Ext.widget('addAccountView');
										openWindow.animateTarget = document.body;
										openWindow.title = "编辑客户";
										openWindow.items.items[0].items.items[0].setValue(rows[0].data["id"]);
										openWindow.items.items[0].items.items[1].setValue(rows[0].data["name"]);
										openWindow.items.items[0].items.items[1].setDisabled(true);
										openWindow.items.items[0].items.items[2].setValue(rows[0].data["phone"]);
										openWindow.items.items[0].items.items[5].setValue(rows[0].data["address"]);
										if(rows[0].data["distributor"]!=null){
											openWindow.items.items[0].items.items[6].setValue(rows[0].data["distributor"]["id"]);
										}
										
										openWindow.show();
									}
									
								}
							}, 
							'button[name=addSubmit]' : {
								click : function(o) {
									var submitFormAdd = o.ownerCt.ownerCt.items.items[0];
									if (!submitFormAdd.getForm().isValid())
										return;
									submitFormAdd.getForm().submit({
												url : 'saveOrupdateAccount.action',
												method : 'POST',
												success : function(form, action) {
													var isSuc = action.result;
													Ext.Msg.alert('提示', isSuc.msg);
													gridStore.load();
													o.ownerCt.ownerCt.close();
												},
												failure : function(form, action) {
													var isSuc = action.result;
													Ext.Msg.alert('提示', isSuc.msg);
												}
											});
								}
							}, 
							'button[name=del]' : {
								click : function(o) {
								//saveOrupdateDistributor
									var rows = o.ownerCt.ownerCt.getSelectionModel().getSelection();// 父组件
									var delIds = "";
									if (rows.length > 0) {
										for (i = 0; i < rows.length; i++) {
											 
											if (i < rows.length - 1) {
												delIds += rows[i].data["id"] + ",";
											} else {
												delIds += rows[i].data["id"];
											}
										}
									} 
									if(delIds!=""){ 
										Ext.Msg.confirm("系统信息", "删除后数据将不可恢复,确定要删除吗", function(btn) {
											if(btn=="yes"){
												Ext.Ajax.request({
													url : "deleteAccounts.action",
													params : {
														"ids" :delIds
													},
													success : function(respose, option) {
														var result = Ext.decode(respose.responseText);
														Ext.Msg.alert('提示', result.msg);
														gridStore.load();
													},
													failure : function(form, action) {
														Ext.Msg.alert("提示", '出错！');
													}
												});
											}
										});
										
									}else{
										Ext.Msg.alert('提示', "请选择需要删除的资源");
									}
								}
							} 
						});
			},
			onLaunch : function() {
				gridStore=this.getAccountsStore();
				gridStore.on('beforeload', function(store, options) { 
	      			Ext.apply(store.proxy.extraParams, {
		      			name:Ext.getCmp('name').getValue(), 
		      			phone:Ext.getCmp('phone').getValue(), 
		      			address:Ext.getCmp('address').getValue() 
	      			});
				});
				gridStore.load();
			}
		});
